/**
 * Created by Administrator on 2016/7/8.
 */
"use strict";
swan_pc
    .controller("AddClipToTaskController",function($scope,$state,ClipPost,$timeout,$rootScope,TimeExchange,TimeCheck,Task) {
        $scope.addclip =
        {
            "actionRemark": null,
            "address": null,
            "advanceInfo": null,
            "category": null,
            "checkList": null,
            "code": null,
            "color": null,
            "deletedAt": null,
            "endTime": null,
            "individualTaskId": null,
            "isDelayed": null,
            "isFixed": null,
            "isImportant": null,
            "isPrincipal": null,
            "isSecret": null,
            "level": null,
            "linkedActivityId": null,
            "linkedActivityType": null,
            "name": null,
            "organizationId": null,
            "ownerId": null,
            "previewTime": null,
            "relatedCompany": null,
            "relatedContact": null,
            "relatedOpp": null,
            "remark": null,
            "remind": null,
            "startTime": null,
            "status": null,
            "type": null,
            "who": null
        },
        $scope.initClipInfo=function(){
            $scope.addclip.individualTaskId=$rootScope.presentTaskid;
        }
        $scope.initClipInfo();
        $scope.status_show = function (option) {
            if (option == "DRAFT" || option == "草稿") return "草稿";
            if (option == "CONFIRM" || option == "确认") return "确认";
            if (option == "CANCELED" || option == "取消") return "取消";
            if (option == "CLOSED" || option == "关闭") return "关闭";
            if (option == "PENDING" || option == "待定") return "待定";
        }
        $scope.judge_show = function (option) {
            if (option == "1") return "是";
            if (option == "0") return "否";
        }
        $scope.init = function () {
            $scope.level_array = [{"key": "主事项", "value": "主事项"}, {"key": "见缝插针", "value": "见缝插针"}];
            $scope.status_array = [{"key": "DRAFT", "value": "草稿"},
                {"key": "CONFIRM", "value": "确认"}, {"key": "CANCELED", "value": "取消"},
                {"key": "CLOSED", "value": "关闭"}, {"key": "PENDING", "value": "待定"}];
        }
        $scope.init();
        $scope.ClipKey = {
            "name": "日程内容",
            "startTime": "开始时间",
            "endTime": "结束时间",
            "address": "日程地点",
            "level": "日程类型",
            "actionRemark": "行动备注",
            "advanceInfo": "高级信息",
            "category": "公事/私事",
            "checkList": "物品检查",
            "code": "编号",
            "color": "颜色",
            "createdBy": null,
            "createdDate": null,
            "deletedAt": "删除时间",
            "id": "ID",
            "individualTaskId": "任务ID",
            "isDelayed": "是否拖延",
            "isFixed": "是否固定时间",
            "isImportant": "是否重要",
            "isPrincipal": "是否属于组织",
            "isSecret": "是否私密",
            "lastModifiedBy": null,
            "lastModifiedDate": null,
            "linkedActivityId": "link活动Id",
            "linkedActivityType": "link活动类型",
            "organizationId": "组织ID",
            "ownerId": "UserID",
            "previewTime": "预计时间",
            "relatedCompany": "公司",
            "relatedContact": "相关联系人",
            "relatedOpp": "机会",
            "remark": "备注",
            "remind": "提醒时间",
            "status": "状态",
            "type": "类型",
            "who": "行动对象"
        }
        $scope.init_show = function () {
            $scope.InfoShow = true;
        }
        $scope.init_show();
        $scope.display = function (option) {
            if (option == 0) {
                $scope.InfoShow = false;
            }
            if (option == 1) {
                $scope.InfoShow = true;
            }
        }
        //console.log($rootScope.clickDate);
        //var clickDate = $rootScope.clickedDate;
        //var clipStartTime = TimeExchange.GMTtoBjTime(clickDate);
        //$scope.addclip.startTime = clickDate;
        //$scope.addclip.endTime = clickDate;
        $scope.TimeShow = function (time) {
            if (time == null) {
                return "";
            }
            var timer = time.toString();
            timer = TimeExchange.StringtoLocalTime(timer);
            return timer;
        };
        $scope.show = function (index) {
            var x = document.getElementById("show" + index);
            //alert(x.value);
            $timeout(function () {
                x.focus();
            });
        };
        $scope.clipStatusValueToKey=function(option){
            angular.forEach($scope.status_array,function(data,index,array){
                if(option==data.value){
                    $scope.addclip.status=data.key;
                }
            })
        };
        $scope.AddToDatabase=function(){
            console.log("AddClipToTask:"+$scope.addclip.startTime+" "+$scope.addclip.endTime);
            if ($scope.addclip.name == null || $scope.addclip.name == "") {
                swal("日程内容不能为空", $scope.addclip.name, "error");
            }else {
                $scope.clipStatusValueToKey($scope.addclip.status);
                if($scope.addclip.status=="DRAFT"){
                    ClipPost.save($scope.addclip,
                        function (result) {
                            $rootScope.calendarDirty = false;
                            swal("添加成功", $scope.addclip.name, "success");
                            $state.go("tasklist");
                        },
                        function (result) {
                            swal("添加失败", $scope.addclip.name, "error");
                        }
                    )
                }else {
                    if ($scope.addclip.level != "主事项" && $scope.addclip.level != "见缝插针") {
                        swal("日程类型必须为主事项或见缝插针", $scope.addclip.name, "error");
                    } else {
                        //console.log("level"+$scope.addclip.level);
                        if ($scope.addclip.level == "主事项") {
                            console.log("AddClipToTask2:" + $scope.addclip.startTime + " " + $scope.addclip.endTime);
                            if ($scope.addclip.startTime == "" || $scope.addclip.startTime == null || $scope.addclip.endTime == "" || $scope.addclip.endTime == null) {
                                swal("主事项开始结束时间不能为空", $scope.addclip.name, "error")
                            } else {
                                var flag = TimeCheck.timecheck($scope.addclip.startTime, $scope.addclip.endTime);
                                if (flag == 0) {
                                    swal("日程不能跨天", $scope.addclip.name, "error");
                                }
                                if (flag == 1) {
                                    swal("日程不能短于30分钟", $scope.addclip.name, "error");
                                }
                                if (flag == 3) {
                                    swal("结束时间不得早于开始时间", $scope.addclip.name, "error");
                                }
                                if (flag == 2) {
                                    ClipPost.save($scope.addclip,
                                        function (result) {
                                            $rootScope.calendarDirty = false;
                                            swal("添加成功", $scope.addclip.name, "success");
                                            $state.go("tasklist");
                                        },
                                        function (result) {
                                            swal("添加失败", $scope.addclip.name, "error");
                                        }
                                    )
                                }
                            }
                        } else {
                            ClipPost.save($scope.addclip,
                                function (result) {
                                    $rootScope.calendarDirty = false;
                                    swal("添加成功", $scope.addclip.name, "success");
                                    $state.go("tasklist");
                                },
                                function (result) {
                                    swal("添加失败", $scope.addclip.name, "error");
                                }
                            )
                        }
                    }
                }
            }
        }
        /*$scope.$watch($scope.addclip.startTime,function(newVlue,oldValue,scope){
            console.log("change:",newValue+" "+ oldValue+" "+scope);
        })*/
        $scope.BackToTaskList = function () {
            //$rootScope.goSref("calendarshow");
            $state.go("tasklist");
        };
    });